﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;
namespace QuanLyThi
{
    public partial class FrmHuyDangKyThi : Form
    {
        public static string mConnect = FrmMain.mConnect;
        private string msql = "";
        public DGC_Library mLibrary = new DGC_Library(mConnect);

        public FrmHuyDangKyThi()
        {
            InitializeComponent();
            this.Load();
            //comboBox1_SelectionChangeCommitted(null, null);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void Load()
        {

            msql = "Select * from tb_CTdaotao order by CT_daotao";
            DataTable dt = mLibrary.GetDataTable(msql, "table");
            cchuongtrinhdt.DataSource = dt;
            cchuongtrinhdt.DisplayMember = "ct_daotao";
            cchuongtrinhdt.ValueMember = "MS_daotao";

         
           
        }

        private void cchuongtrinhdt_SelectionChangeCommitted(object sender, EventArgs e)
        {
            msql = "Select a.* from tb_Khoadaotao a " +
                  " inner join tb_cauhinhCtdaotao b " +
                  " on a.MS_Khoadaotao=b.ms_Khoadaotao " +
                  " Where b.ms_daotao="+cchuongtrinhdt.SelectedValue.ToString();
            DataTable dt1 = mLibrary.GetDataTable(msql, "table");
            ckhoa.DataSource = dt1;
            ckhoa.DisplayMember = "khoadaotao";
            ckhoa.ValueMember = "MS_khoadaotao";
        }

        private void ckhoa_SelectionChangeCommitted(object sender, EventArgs e)
        {
            msql = "Select * from tb_Lopdaotao Where MS_Khoadaotao="+ckhoa.SelectedValue.ToString();
            DataTable dt2 = mLibrary.GetDataTable(msql, "table");
            clop.DataSource = dt2;
            clop.DisplayMember = "tenlop";
            clop.ValueMember = "MS_lop";
        }

        private void clop_SelectionChangeCommitted(object sender, EventArgs e)
        {          
            //showmon thi
            msql = "Select * from tb_mondaotao a " +
                " inner join TB_cauhinhlop b " +
                " on a.ms_mondaotao=b.ms_mondaotao";
            DataTable dtmon = mLibrary.GetDataTable(msql, "123");
            cmon.DataSource = dtmon;
            cmon.DisplayMember = "mondaotao";
            cmon.ValueMember = "ms_mondaotao";

            
            
            //string mslop = clop.SelectedValue.ToString();
            //string mskhoa = ckhoa.SelectedValue.ToString();
            //msql = "Select a.ms_hssv,a.hovaten,a.gioitinh,a.namsinh,a.diachi,a.dienthoai,a.mail from tb_hssv a " +
            //       " inner join tb_hssvlop b on a.ms_hssv=b.ms_hssv " +
            //       " where b.ms_lop='"+clop.SelectedValue.ToString()+"' Order by a.hovaten";
            //DataTable dt1 = mLibrary.GetDataTable(msql, "tb1");
            //dataGridView1.AutoGenerateColumns = false;
            //dataGridView1.DataSource = dt1;           
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                string makhoa = ckhoa.SelectedValue.ToString();
                msql = "Select * from tb_Lopdaotao Where Ngaydangky<=getdate() " +
                        " and Getdate()<=Ngayketthuc and MS_Khoadaotao=" + makhoa;
                DataTable dt = mLibrary.GetDataTable(msql, "ASD");
                clop.DataSource = dt;
                clop.DisplayMember = "tenlop";
                clop.ValueMember = "ms_lop";
            }
            else
            {   
                string makhoa = ckhoa.SelectedValue.ToString();
                msql = " Select * from tb_Lopdaotao Where MS_Khoadaotao=" + makhoa;
                DataTable dt = mLibrary.GetDataTable(msql, "ASD");
                clop.DataSource = dt;
                clop.DisplayMember = "tenlop";
                clop.ValueMember = "ms_lop";
            }
        }



   
        private string LayMaHSSV()
        {
            string re = "'";
            DataTable dt = (DataTable)dataGridView1.DataSource;
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i <= dt.Rows.Count - 1; i++)
                {
                    string Ma = "'";
                    if (dt.Rows[i]["MS_hssv"].ToString() == null)
                        Ma = "0";
                    else
                        Ma = dt.Rows[i]["MS_hssv"].ToString();
                    re = re + "','" + Ma;
                }
            }
            return re+"'";
        }

        private void butLuu_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dialog = MessageBox.Show("Bạn có muốn lưu thông tin này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialog == DialogResult.Yes)
                {
                    string malop = clop.SelectedValue.ToString();                  
                    //cập nhật thông tin mới
                    string MaKhoaDT = LayMaHSSV();
                    msql = "Delete tb_hssvlop Where ms_lop='" + malop + "' and MS_hssv not in("+MaKhoaDT+")";
                    mLibrary.RunProc(msql);
                    MessageBox.Show("Lưu thông tin thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Lưu thông tin không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }     
        }

        private void butLuu_Click_1(object sender, EventArgs e)
        {
            butLuu_Click(null, null);
        }

        private void cmon_SelectionChangeCommitted(object sender, EventArgs e)
        {
            msql = "Select * from tb_loaihinhthi a " +
                  "  Inner join tb_cauhinh_loaihinhthi b " +
                  "  on a.ms_loaithi=b.ms_loaithi where ms_mondaotao=" + cmon.SelectedValue.ToString();
            chinhthucthi.DataSource = mLibrary.GetDataTable(msql,"a");
            chinhthucthi.DisplayMember = "tenloaithi";
            chinhthucthi.ValueMember = "ms_loaithi";
        }

        private void chinhthucthi_SelectionChangeCommitted(object sender, EventArgs e)
        {
            string malop = clop.SelectedValue.ToString();
            string mon = cmon.SelectedValue.ToString();
            string loaithi = chinhthucthi.SelectedValue.ToString();

            msql = "Select 0 as 'dk',b.ms_lop,b.ngaydangky,a.MS_HSSV,a.hovaten, case a.gioitinh When 1 then N'Nam' Else N'Nữ'" +
               " End as Gioitinh, a.namsinh,a.diachi,a.dienthoai,a.mail from  tb_hssv a inner join tb_hssvlop b " +
               " on a.ms_hssv=b.ms_hssv Inner join tb_hssvthi c on a.ms_hssv=c.ms_hssv and b.ms_lop=c.ms_lop " +
               " Where c.lanthi=1 and c.ketqua is Null and b.ms_lop='" + malop + "' and c.Ms_mondaotao=" + mon + " and ms_loaithi= " + loaithi +
               " Order by  a.hovaten";
            DataTable dt = mLibrary.GetDataTable(msql, "a");
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = dt;
                
              
        }

        private void button6_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void butLuu_Click_2(object sender, EventArgs e)
        {
            try
            {
                DialogResult dialog = MessageBox.Show("Bạn có muốn lưu thông tin này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialog == DialogResult.Yes)
                {
                    DataTable dt = (DataTable)dataGridView1.DataSource;
                    foreach (DataRow row in dt.Rows)
                    {
                        if (row["dk"].ToString() == "1")
                        {
                            string ms_lop = clop.SelectedValue.ToString();
                            string ms_hssv = row["ms_hssv"].ToString();
                            string ms_mondaotao = cmon.SelectedValue.ToString();
                            string ms_loaithi = chinhthucthi.SelectedValue.ToString();
                       
                            //cập nhật thông tin mới

                            msql = " Delete from tb_hssvthi Where ms_lop='" + ms_lop +
                                "' and MS_HSSV='" + ms_hssv +
                                "' and MS_Mondaotao=" + ms_mondaotao
                                + " and MS_Loaithi=" + ms_loaithi 
                                + " and lanthi=1";
                            mLibrary.RunProc(msql);
                        }
                      
                    }
                 
                    MessageBox.Show("Lưu thông tin thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    chinhthucthi_SelectionChangeCommitted(null, null);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Lưu thông tin không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }    
        }


      

      

         
     
    }
}
